<?php defined("MAIN_DIR") or die("No direct access");

$box_id = UV::readInt($route->getPath(4));

$edit_error = false;
if (isset($_POST['action']) && $_POST['action'] == "edit") {
	$days = $sizes = $cost = "";
	for ($i=0; $i <= $_POST['sizes']; $i++)
	{
		for ($j=0; $j <= $_POST['days']; $j++)
		{
			if ($j == 0 && $i == 0)
				;
			elseif ($i == 0)
			{
				$days .= $_POST['day_'.$j];
				if ($j < $_POST['days'])
					$days .= ",";
			}
			elseif ($j == 0)
			{
				$sizes .= $_POST['size_'.$i];
				if ($i < $_POST['sizes'])
					$sizes .= ",";
			}
			else
			{
				$cost .= $_POST['cost_'.$j.'_'.$i];
				if ($j < $_POST['days'] || $i < $_POST['sizes'])
					$cost .= ",";
			}
		}
	}
	
	$address = trim(filter_var($_POST['address'],FILTER_SANITIZE_MAGIC_QUOTES));
	$phone = trim(filter_var($_POST['phone'],FILTER_SANITIZE_MAGIC_QUOTES));
	$days = trim(filter_var($days,FILTER_SANITIZE_MAGIC_QUOTES));
	$sizes = trim(filter_var($sizes,FILTER_SANITIZE_MAGIC_QUOTES));
	$cost = trim(filter_var($cost,FILTER_SANITIZE_MAGIC_QUOTES));
	
	$edit_query = "update `box` set `bank_id`='$_POST[bank]',`address`='$address',`phone`='$phone',`days`='$days',`dimensions`='$sizes',`cost`='$cost' where `box_id` = '$_POST[box_id]' limit 1";
	if (!mysql_query($edit_query))
		$edit_error = true;
}

if ($box_id) {
	$box_res = mysql_query("select `B`.`name`,`Bx`.* from `box` as `Bx`,`bank` as `B` where `box_id` = '$box_id' and `B`.`bank_id` = `Bx`.`bank_id` limit 1");
	
	if (mysql_num_rows($box_res) == 0) {
		to("/admin/boxes/");
	}
	
	$box_row = mysql_fetch_array($box_res);
	$days = explode(",",$box_row['days']);
	$sizes = explode(",",$box_row['dimensions']);
	$cost = explode(",",$box_row['cost']);
	
	$city_path = $db->getCell("select `C`.`path` from `city` as `C`,`bank` as `B` where `B`.`bank_id` = '$box_row[bank_id]' and `B`.`city_id` = `C`.`city_id` limit 1");
}

require_once ADMIN.HEADFILE;
?>
<style>
.right_line { border-right: 1px #888888 dotted }
.bottom_line { border-bottom: 1px #888888 dotted }
#box input { width: 110px; }
.buttons span
{
	cursor: pointer;
	text-decoration: underline;
	line-height: 25px;
}
</style>
<script type="text/javascript" src="/files/js/admin/boxes.js"></script>
<div align="left">
<h1>Редактирование ячейки</h1>
<hr /><?php
if (isset($_POST['action']) && $_POST['action'] == "edit" && !$edit_error)
	echo "<p style='color:green'>Успешно отредактировано</p>";
elseif ($edit_error)
	echo "<p style='color:red'>Не удалось отредактировать</p>";
?><form method='POST'>
<input type='hidden' name='action' value='edit' />
<input type='hidden' name='box_id' value='<?php echo $box_row['box_id']; ?>' />
<input type='hidden' name='days' id='days' value='<?php echo count($days); ?>' />
<input type='hidden' name='sizes' id='sizes' value='<?php echo count($sizes); ?>' />
<table cellpadding='5' cellspacing='0'>
	<tr>
		<td width="150px">Ссылка на сайте</td>
		<td width="500px"><a href='<?php echo "/$city_path/banks/boxes/".$box_row['bank_id'];?>' target="_blank"><?php echo $box_row['name'];?></a></td>
	</tr>
	<tr>
		<td>ID</td>
		<td><input style="width:200px" type='text' name='box_id' value='<?php echo $box_row['box_id'];?>' disabled /></td>
	</tr>
	<tr>
		<td>Банк</td>
		<td>
			<select style="width:200px" name='bank'><?php
			$bank_res = mysql_query("select `bank_id`,`name` from `bank` where `city_id` = '$_SESSION[city_admin]' order by `name`");
			while ($bank_row = mysql_fetch_array($bank_res))
			{
				echo "<option value='$bank_row[bank_id]'";
				if ($bank_row['bank_id'] == $box_row['bank_id'])
					echo " selected='selected'";
				echo ">$bank_row[name]</option>";
			}
			?></select>
		</td>
	</tr>
	<tr>
		<td width="150px">Адрес</td>
		<td width="500px">
			<input style='width:100%' type='text' name='address' value='<?php echo $box_row['address'];?>' />
		</td>
	</tr>
	<tr>
		<td width="150px">Телефон</td>
		<td width="500px">
			<input style='width:100%' type='text' name='phone' value='<?php echo $box_row['phone'];?>' />
		</td>
	</tr>
	<tr>
		<td colspan='2' style="border:0px gray solid">
			<table cellpadding="5" cellspacing="0" id="box">
				<?php
				$cost_count = 0;
				for ($i = 0; $i <= count($sizes); $i++)
				{
					echo "<tr id=$i>";
					for ($j = 0; $j <= count($days); $j++)
					{
						if ($i == 0 && $j == 0)
							echo "<td class='bottom_line right_line'>Размеры \ Период</td>";
						elseif ($i == 0)
							echo "<td class='bottom_line'><input type='text' name='day_$j' value='".$days[$j-1]."' /></td>";
						elseif ($j == 0)
							echo "<td class='right_line'><input type='text' name='size_$i' value='".$sizes[$i-1]."' /></td>";
						else
							echo "<td><input type='text' name='cost_".($j)."_".($i)."' value=".$cost[$cost_count++]." /></td>";
					}
					echo "</tr>";
				}
			?></table>
			<div class="buttons">
				<span id="add_day">Добавить период</span> - <span id="del_day">Убрать период</span><br />
				<span id="add_size">Добавить размер</span> - <span id="del_size">Убрать размер</span>
			</div>
		</td>
	</tr>
	<tr valign='top'>
		<td colspan='2' align="right"><button style="width:100px;height:30px" type="submit">Сохранить</button></td>
	</tr>
</table>
</form>
<br /><hr /><br />
</div>

<?php require_once ADMIN.FOOTERFILE; ?>